exports.bulk_purchase_pay_success = function (req, res) {

    //获取restful参数
    var restfulParams = req.routeInfo.restfulParams;

    var resultData = {};

    var rs = {};

    var diffCount = 0;

    var isHead = false; //是否为团长

    var couponData = {};

    var resultData = {};


    //任务流创建
    async.waterfall([

        //1.拼团活动
        function (cb) {

            if(restfulParams.bpau_id == 0 || restfulParams.bpau_id == undefined){

              
                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.bulk_purchase_join_user + "/bpau_uid/"+restfulParams.u_id + "/bpau_oaid/" + restfulParams.oa_id + "/sort_order/bpau_join_time-desc/skip/0/limit/1",
                        method: 'POST'
                    },
                    function (err, data) {

                        console.log(111,data);
                    
                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
                     
                        if(data.data.length <= 0){
                            return RES.response(res, false,"用户活动参与信息查询失败");
                        }

                        resultData = data.data[0];

                        console.log(9999,resultData);

                        //判断是否为团长
                        if(resultData.bpau_fid == 0){

                            isHead = true;

                            rs.diffCount = resultData.bpau_member_count - resultData.bpau_user_count;
                            rs.bpau_uid              = resultData.bpau_id;
                            rs.oa_id                 = restfulParams.oa_id;
                            rs.bpau_member_count     = resultData.bpau_member_count;
             
                            cb();

                        }else{

                            isHead = false;

                            //需要查询团长信息
                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.bulk_purchase_join_user + "/bpau_id/" + resultData.bpau_fid,
                                    method: 'POST'
                                },
                                function (err, data) {

                                    console.log(222,data);

                                    
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }
                      
                                    resultData = data.data[0];
                
                                    rs.diffCount = resultData.bpau_member_count - resultData.bpau_user_count;
                                    rs.bpau_uid              = resultData.bpau_id;
                                    rs.oa_id                 = restfulParams.oa_id;
                                    rs.bpau_member_count     = resultData.bpau_member_count;
                                
                                    cb();
                
                                }
                            );


                        }
                    }
                );

                

            }else{

               

                R.SEND_HTTP(
                    req,
                    {
                        url: CORE_APIConfig.coreAPI.bulk_purchase_join_user + "/bpau_id/"+restfulParams.bpau_id,
                        method: 'POST'
                    },
                    function (err, data) {

                        console.log(8888,data);

                        //判断是否有错误信息
                        if (err) {
                            return cb(err, data);
                        }
    
                      
                        resultData = data.data[0];
    
                        rs.diffCount             = resultData.bpau_member_count - resultData.bpau_user_count;
                        rs.bpau_uid              = resultData.bpau_id;
                        rs.header_url            = resultData.u_header_url;
                        rs.oa_id                 = resultData.bpau_oaid;
                        rs.bpau_member_count     = resultData.bpau_member_count;

                        cb();
    
                    }
                );

            }
            
        },

        //查询用户信息
        function(cb){

            console.log(CORE_APIConfig.coreAPI.userSearch + "/u_id/" + rs.bpau_uid)

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.userSearch + "/u_id/" + rs.bpau_uid,
                    method: 'POST',
                    data: {
                        fields: ["u_nickname","u_header_url"].join(",")
                    }
                },
                function (err, data) {

                    console.log(data);


                    rs.userInfo = data.data[0];

                    cb();

                }
            );

        },

        //获取临时二维码
        function(cb){

            var qrUrl = "";
            if(resultData.bpau_member_count == 0){
                qrUrl = "http://127.0.0.1:3400/weixin/temporaryQrCode/sceneStr/jielong|&oa_id=" + resultData.bpau_oaid + "&o_activity_type=0";
            }else{
                qrUrl = "http://127.0.0.1:3400/weixin/temporaryQrCode/sceneStr/pintuan|"+resultData.bpau_oaid+"/bpau_uid/"+resultData.bpau_id+"/oa_id/"+resultData.bpau_oaid+"/bpau_member_count/"+resultData.bpau_member_count;
            }

            console.log(qrUrl);

            R.SEND_HTTP(
                req,
                {
                    url : qrUrl,
                    // url:"http://127.0.0.1:3400/weixin/temporaryQrCode/sceneStr/"+sceneStr+"|"+resultData.bpau_oaid+"/bpau_uid/"+resultData.bpau_id+"/oa_id/"+resultData.bpau_oaid+"/bpau_member_count/"+resultData.bpau_member_count,
                    method:"GET"
                },
                function (err,data) {
                    // //console.log("----------------");
                    // //console.log(data)
                    if (err) {
                        return cb();
                    }
                    rs.qrCode = data;
                    cb();
                }
            )
        },

        function(cb){

            //console.log("是否为团长:" + isHead);

            rs.couponData = [];
            //rs.qrCode     = "https://cdn2.jianshu.io/assets/web/download-apps-page-top-qrcode-9b3161f68985dc5c1f8f6bdb36a4bd4d.png";

            console.log(CORE_APIConfig.coreAPI.searchBulkPurchaseActivityConfig + "/bpac_oaid/"+restfulParams.oa_id);

            //查询是否有优惠卷
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.searchBulkPurchaseActivityConfig + "/bpac_oaid/"+restfulParams.oa_id,
                    method: 'POST',
                    data:{
                        fields : ["bpac_builder_coupon_id","bpac_builder_coupon_type","bpac_participant_coupon_id","bpac_participant_coupon_type"].join(",")
                    }
                },
                function (err, data) {

                    console.log(333,data);

                    //判断是否有错误信息
                    if (err) {
                        return cb();
                    }

                    var coupon = data.data[0];

                    //判断是开团还是参团
                    if(isHead){

                        //查询优惠卷信息
                        if(coupon.bpac_builder_coupon_id == 0){
                            return cb();
                        }

                        //查询优惠卷信息
                        if(coupon.bpac_builder_coupon_type == 0){

                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_cash_coupon+"/cc_id/"+coupon.bpac_builder_coupon_id,
                                    method: 'GET',
    
                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }

                                    //console.log("1111" + data);

                                    rs.couponData = data.data;


                                    //console.log(rs.couponData)


                                    cb();
    
                                }
                            );

                        }else if(coupon.bpac_builder_coupon_type == 1){

                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_course_coupon+"/cc_id/"+coupon.bpac_builder_coupon_id,
                                    method: 'GET',
    
                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }
                                    //console.log("2222" + data);
                                    rs.couponData = data.data;

                                    cb();
    
                                }
                            );

                            
                        }else if(coupon.bpac_builder_coupon_type == 2){

                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_exchange_coupon+"/ec_id/"+coupon.bpac_builder_coupon_id,
                                    method: 'GET',
    
                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }
                                    //console.log("3333" + data);
                                    rs.couponData = data.data;

                                    cb();
    
                                }
                            );

                        }else{
                            return cb();
                        }

                    }else{

                        //查询优惠卷信息
                        if(coupon.bpac_participant_coupon_id == 0){
                            return cb();
                        }

                        //查询优惠卷信息
                        if(coupon.bpac_participant_coupon_type == 0){

                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_cash_coupon+"/cc_id/"+coupon.bpac_participant_coupon_id,
                                    method: 'GET',
    
                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }

                                    rs.couponData = data.data;

                                    cb();
    
                                }
                            );

                        }else if(coupon.bpac_participant_coupon_type == 1){

                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_course_coupon+"/cc_id/"+coupon.bpac_participant_coupon_id,
                                    method: 'GET',
    
                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }

                                    rs.couponData = data.data;

                                    cb();
    
                                }
                            );

                            
                        }else if(coupon.bpac_participant_coupon_type == 2){

                            R.SEND_HTTP(
                                req,
                                {
                                    url: CORE_APIConfig.coreAPI.search_exchange_coupon+"/ec_id/"+coupon.bpac_participant_coupon_id,
                                    method: 'GET',
    
                                },
                                function (err, data) {
                                    //判断是否有错误信息
                                    if (err) {
                                        return cb(err, data);
                                    }

                                    rs.couponData = data.data;

                                    cb();
    
                                }
                            );

                        }else{
                            return cb();
                        }
                    }
                }
            );
        },
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_organization_activity_info + "/oa_id/" + restfulParams.oa_id,
                    method: 'POST',
                    data:{
                        fields:["oa_id","oa_title","oa_share_title","oa_share_content","oa_share_pic",'o_name',"oa_end_time"].join(",")
                    }
                },
                function (err, data) {

                    //console.log(data);

                    //判断是否有错误信息
                    if (err) {
                        return cb(err, data);
                    }

                    rs.activityShareData = data.data[0];

                    var activityShareData = rs.activityShareData;

                    rs.activityEndtime = activityShareData.oa_end_time;
                    cb();
                }
            );

        },
        function (cb) {

            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.search_order + "/o_id/" + restfulParams.o_id,
                    method: 'POST',
                    data: {
                        fields: [ "o_custom"].join(",")
                    }
                },
                function (err, data) {

                    rs.o_custom = data.data[0].o_custom;

                    cb();

                }
            );

        },
        function (cb) {
            R.SEND_HTTP(
                req,
                {
                    url: CORE_APIConfig.coreAPI.searchOrganizationActivity + "/oa_id/" + restfulParams.oa_id,
                    method: 'POST',
                    data: {
                        fields: ["oa_join_count"].join(",")
                    }
                },
                function (err, data) {

                    var joinCount = data.data[0].oa_join_count;

                    R.SEND_HTTP(
                        req,
                        {
                            url: CORE_APIConfig.coreAPI.search_bulk_purchase_activity_config + "/bpac_oaid/" + restfulParams.oa_id,
                            method: 'POST',
                            data: {
                                fields: [ "bpac_max_user_count"].join(",")
                            }
                        },
                        function (err, data) {

                            var joinCountMax = data.data[0].bpac_max_user_count;


                            if(parseInt(joinCount)<parseInt(joinCountMax)){
                                rs.member_full = 0;
                            }else{
                                rs.member_full = 1;
                            }
                            cb()
                        }
                    );

                }
            );
        }

    ], function (err, result) {

        //返回最终拼接结果
        RES.response(res, true, rs);

    }
);

}